#!/usr/bin/env python
import pygtk
pygtk.require("2.0")
import gtk, gobject
from location import Location
from share import *
from window_template import WindowTemplate

###
protocols = ["samba","ssh","nfs","ftp"]
###
        
class ShareWindow(WindowTemplate):
    def __init__(self,locations,share=None):
        """constructor"""
        WindowTemplate.__init__(self)
        self.locations = locations
        self.create_window()
        if share:
            self.window.set_title("Share Settings")
            self.share = share
            self.fill()
        else:
            self.window.set_title("Add Share")
            self.share = Share("temp")
            self.__set_protocol()
            self.__set_location()
        
        self.ddl_protocol.connect("changed",self.ddl_protocol_changed_callback)    
        self.window.show_all()
    
    def create_window(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.global_vbox = gtk.VBox(False,5)
        self.window.add(self.global_vbox)
        
        #Protocol (Drop Down List)
        self.lbl_protocol = gtk.Label("Protocol:")
        self.ddl_protocol = gtk.combo_box_new_text()
        for protocol in protocols:
            self.ddl_protocol.append_text(protocol)
        self.global_vbox.pack_start(self.build_hbox((self.lbl_protocol,self.ddl_protocol)))
        
        #Name
        self.lbl_name = gtk.Label("Name :")
        self.tb_name = gtk.Entry()
        self.add_validator(self.tb_name,"REGEX_NAME")
        self.global_vbox.pack_start(self.build_hbox((self.lbl_name,self.tb_name)))
        
        #Location (Drop Down List)
        self.lbl_location = gtk.Label("Location :")
        self.ddl_location = gtk.combo_box_new_text()
        
        for location in self.locations:
            self.ddl_location.append_text(self.locations[location].name)
        self.global_vbox.pack_start(self.build_hbox((self.lbl_location,self.ddl_location)))

        #Mount point
        self.lbl_mount_point = gtk.Label("Mount_Point :")
        self.tb_mount_point = gtk.Entry()
        self.add_validator(self.tb_mount_point,"REGEX_MOUNT_POINT")
        self.global_vbox.pack_start(self.build_hbox((self.lbl_mount_point,self.tb_mount_point)))

        #Server
        self.lbl_server = gtk.Label("Server :")
        self.tb_server = gtk.Entry()
        self.add_validator(self.tb_server,"REGEX_NAME")
        self.global_vbox.pack_start(self.build_hbox((self.lbl_server,self.tb_server)))
        
        #Path
        self.lbl_path = gtk.Label("Path :")
        self.tb_path = gtk.Entry()
        self.add_validator(self.tb_path,"REGEX_PATH")
        self.global_vbox.pack_start(self.build_hbox((self.lbl_path,self.tb_path)))
        
        #Domain
        self.lbl_domain = gtk.Label("Domain :")
        self.tb_domain = gtk.Entry()
        self.add_validator(self.tb_domain,"REGEX_NAME",empty = True)    
        self.global_vbox.pack_start(self.build_hbox((self.lbl_domain,self.tb_domain)))
        #Login
        self.lbl_login = gtk.Label("Login :") 
        self.tb_login = gtk.Entry()
        self.add_validator(self.tb_login,"REGEX_NAME",empty = True)    
        self.global_vbox.pack_start(self.build_hbox((self.lbl_login,self.tb_login)))
        
        #Path
        self.lbl_password = gtk.Label("Password :")
        self.tb_password = gtk.Entry()
        self.add_validator(self.tb_password,"REGEX_NAME",True)
        self.tb_password.set_visibility(False)
        self.global_vbox.pack_start(self.build_hbox((self.lbl_password,self.tb_password)))
        
        #Automount
        self.cb_automount = gtk.CheckButton("Automount")
        self.global_vbox.pack_start(self.cb_automount)
        
        #Submit Button
        submit_button = gtk.Button(None,gtk.STOCK_APPLY)
        submit_button.connect("clicked",self.submit)
        self.global_vbox.pack_end(submit_button,False)
        
    def fill(self):
        self.tb_name.set_text(self.share.name)
        self.tb_mount_point.set_text(self.share.mount_point)
        self.tb_server.set_text(self.share.server)
        self.tb_path.set_text(self.share.path)
        self.cb_automount.set_active(self.share.automount)
        self.tb_domain.set_text(self.share.domain)
        self.tb_login.set_text(self.share.login)
        self.tb_password.set_text(self.share.password)
        self.__set_protocol(self.share.protocol)
        self.__set_location(self.share.location.name)
        
        
    #Private methods
    def __get_protocol(self):
        return self.ddl_protocol.get_model().get_value(self.ddl_protocol.get_active_iter(),0)
    
    def __get_location(self):
        for location in self.locations:
            if self.ddl_location.get_model().get_value(self.ddl_location.get_active_iter(),0) == self.locations[location].name:
                return self.locations[location]
    
    def __set_protocol(self,protocol=None):
        self.ddl_protocol.set_active(0)
        if protocol:
            while self.ddl_protocol.get_active_text() != protocol:
                self.ddl_protocol.set_active(self.ddl_protocol.get_active() + 1)
            
    def __set_location(self,location_name=None):
        self.ddl_location.set_active(0)
        if location_name:
            while self.ddl_location.get_active_text() != location_name:
                self.ddl_location.set_active(self.ddl_location.get_active() + 1)
            
    #Callbacks
    def ddl_protocol_changed_callback(self,widget=None):
        if self.__get_protocol() == "nfs":
            for item in (self.lbl_domain,self.tb_domain,self.lbl_login,self.tb_login,self.lbl_password,self.tb_password):
                item.hide()
        elif self.__get_protocol() in ("ssh","ftp"):
            for item in (self.lbl_domain,self.tb_domain):
                item.hide()
            for item in (self.lbl_login,self.tb_login,self.lbl_password,self.tb_password):
                item.show()
        elif self.__get_protocol() == "samba":
            for item in (self.lbl_domain,self.tb_domain,self.lbl_login,self.tb_login,self.lbl_password,self.tb_password):
                item.show()
    
    def submit(self,widget = None):
        if self.is_valid():
            self.share.name = self.tb_name.get_text()
            self.share.mount_point = self.tb_mount_point.get_text()
            self.share.server = self.tb_server.get_text()
            self.share.path = self.tb_path.get_text()
            self.share.login = self.tb_login.get_text()
            self.share.password = self.tb_password.get_text()
            self.share.domain = self.tb_domain.get_text()

            self.share.automount = self.cb_automount.get_active()
            self.share.protocol = self.__get_protocol()
            self.share.set_default_port()
            self.share.location = self.__get_location()
            self.window.destroy()
    
    def main(self):
        gtk.main()
        return self.share
        
    def delete_event(self,widget,event):
        self.share = None
        return False
    
    def destroy(self,widget):
        gtk.main_quit()
    
if __name__ == "__main__":
    loc1 = Location(1)
    loc1.name = "loc1"
    loc2 = Location(2)
    loc2.name = "loc2"
    locations = {1:loc1,2:loc2}
    share = Share(3)
    share.name = "test"
    share.protocol = "samba"
    share.location = loc2
    share.mount_point = "/mnt/test"
    share.server = "server-de-test"
    share.path = "path/to/test/share"
    share.domain = "esi-supinfo.com"
    share.login = "62142"
    share.password = "prout"
    test = ShareWindow(locations,share)
    new_share = test.main()
    print new_share.name